package com.leo.tool.serialize.json;

import java.io.File;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.SneakyThrows;

/**
 * @author Leo
 * @date 2019/07/07
 */
public class JacksonSerializer {

    /** ObjectMapper线程安全 */
    private static ObjectMapper mapper = new ObjectMapper();

    @SneakyThrows
    public static byte[] serialize(Object obj) {
        return mapper.writeValueAsBytes(obj);
    }

    @SneakyThrows
    public static void serialize(String path, Object obj) {
        mapper.writeValue(new File(path), obj);
    }

    @SneakyThrows
    public static <T> T deserialize(byte[] bytes, Class<T> valueType) {
        return mapper.readValue(bytes, valueType);
    }

    @SneakyThrows
    public static <T> T deserialize(String path, Class<T> valueType) {
        return mapper.readValue(new File(path), valueType);
    }

}
